<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/3/11
 * Time: 9:41
 */
namespace app\api\controller;
use think\Db;
class Friend extends Base {
    /**
     * 异地登录验证
     * @return string
     */
//    public function __construct()
//    {
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == 112){
//            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
//            exit;
//        }elseif($auth == 110){
//            echo json_encode(['status' => 302, 'msg' => $this->frost]);
//            exit;
//        }
//    }

    //好友申请
    public function index(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $type = input('param.type')?input('param.type'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        if($type==1){//待申请
            $where['other_user_id'] = $user_id;
            $where['friend_status'] = 1;
            $lists = Db::table('up_friend')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->field('uuu.user_gender,a.friend_time,a.friend_id,a.user_id,a.other_user_id,uuu.user_username as name,uuu.user_image')
                ->limit($limit,$num)
                ->where($where)
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['user_gender'] == 1){
                    $vo['vip'] = $this->getVip($vo['user_id']);
                }else{
                    $vo['vip'] = 1;
                }
            }
            return  json(array('status'=>200,'msg'=>$lists));
        }elseif ($type ==2){//已经是好友
            $where1['a.other_user_id'] = $user_id;
            $where1['a.friend_status'] = 2;
            $lists = Db::table('up_friend')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->field('uuu.user_gender,a.friend_id,a.user_id,a.other_user_id,uuu.user_username as name,uuu.user_image')
                ->limit($limit,$num)
                ->where($where1)
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['user_gender'] == 1){
                    $vo['vip'] = $this->getVip($vo['user_id']);
                }else{
                    $vo['vip'] = 1;
                }
            }
            foreach($lists as $k => $v){
                if(empty($v['name'])){
                    $lists[$k]['name'] = $v['name'];
                }
                // 字母排序
                $lists[$k]['letter'] = $this->Getzimu($lists[$k]['name']);
            }
            $new_names = array_column($lists, 'letter');
            array_multisort($new_names, SORT_ASC, $lists);
            $start = '';
            $res = array();
            foreach ($lists as $k => $v) {
                if ($v['letter'] != $start) {
                    $start = $v['letter'];
                }
                $res[$start][] = $v;
            }
            $jing = array();
            if(isset($res['#'])){
                $jing['#'] = $res['#'];
                unset($res['#']);
            }
            $friendList = array_merge($res,$jing);
            return  json(array('status'=>200,'msg'=>$friendList));

        }

    }
    //拆礼物列表
    public function demolitionList(){
        extract(input());
        $other_user_id = input('param.other_user_id');
        $where['a.user_id'] = $other_user_id;
        $where['a.other_user_id'] = $user_id;
        $list = Db::table('up_friend')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->join('up_gift ug','ug.gift_id = a.gift_id','left')
            ->field('uuu.user_username,ug.gift_img,a.friend_id,a.friend_glod,a.friend_num')
            ->where($where)->find();
        return  json(array('status'=>200,'msg'=>$list));
    }

    //同意好友  拒绝好友
    public function addFriend(){
        extract(input());
        $friend_id = input('param.friend_id');
        $type = input('param.type');//1同意  2拒绝
        //查询数据
        $info = Db::table('up_friend')->where('friend_id',$friend_id)->find();
        //查询本人的性别
        $info1 = Db::table('up_u_user')->where('user_id',$info['user_id'])->find();
        //查询要添加的好友的性别
        $info2 = Db::table('up_u_user')->where('user_id',$info['other_user_id'])->find();
        if($type==1){//同意
            //更新列表状态
            Db::table('up_friend')->where('friend_id',$friend_id)->setField('friend_status',2);

            //插入朋友表
            $data2['user_id'] = $info['other_user_id'];
            $data2['other_user_id'] = $info['user_id'];
            $data2['friend_status'] = 2;
            $data2['friend_time'] = date('Y-m-d H:i:s',time());
            $res1 = Db::table('up_friend')->insertGetId($data2);

            if($info1['user_gender']==1&&$info2['user_gender']==2){
                if($info['privately']==2){//不是私下赠送,要入用户积分版

                    //被申请人要加积分
                    Db::table('up_u_user')->where('user_id',$info['other_user_id'])->setInc('user_integral',$info['friend_glod']);
                }
                //本人要加积分
//                Db::table('up_u_user')->where('user_id',$info['user_id'])->setInc('user_integral',$info['friend_glod']);
                //更新消费记录
                Db::table('up_u_user')->where('user_id',$info['user_id'])->setInc('user_consume',$info['friend_glod']);
                //增加男用户送礼物金币
                Db::table('up_u_user')->where('user_id',$info['user_id'])->setInc('user_gift_gold',$info['friend_glod']);
                //更新被申请人账户余额
                Db::table('up_u_user')->where('user_id',$info['other_user_id'])->setInc('user_balance',$info['friend_glod']);
                //更新被申请人礼物余额
                Db::table('up_u_user')->where('user_id',$info['other_user_id'])->setInc('user_gift_gold',$info['friend_glod']);
            }
            if($res1){
                return  json(array('status'=>200,'msg'=>"成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"失败"));
            }
        }elseif ($type==2){//拒绝

            if($info1['user_gender']==1&&$info2['user_gender']==2){
                //减去用户表已经扣除的送出礼物的金币，更新 用户的账户余额
                Db::table('up_u_user')->where('user_id',$info['user_id'])->setInc('user_balance',$info['friend_glod']);
//                Db::table('up_u_user')->where('user_id',$info['user_id'])->setDec('user_gift_gold',$info['friend_glod']);
            }

            $res1 = Db::table('up_friend')->where('friend_id',$friend_id)->delete();
            if($res1){
                return  json(array('status'=>200,'msg'=>"成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"失败"));
            }
        }
    }
    //聊天送礼物
    public function present(){
        extract(input());
        $num = input('param.num');
        $price = input('param.price');
        $other_user_id = input('param.other_user_id');
        $gift_id = input('param.gift_id');
        $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
        if($price>$user_balance) {
            return json(array('status' => 100, 'msg' => "金币不足,请先充值"));
        }
        Db::startTrans();
        //本人要加积分
//        $res1 = Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_integral',$price);
        //本人要扣除金币数量
        $res2 = Db::table('up_u_user')->where('user_id',$user_id)->setDec('user_balance',$price);
        //更新男性用户消费记录  todo
        Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_consume',$price);
        Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_gift_gold',$price);



        $data['present_user_id'] = $user_id;
        $data['present_other_user_id'] = $other_user_id;
        $data['present_gift'] = $gift_id;
        $data['present_glod'] = $price;
        $data['present_num'] = $num;
        $data['present_time'] = date('Y-m-d H:i:s',time());
        //插入送入礼物表
        $res = Db::table('up_present')->insertGetId($data);

        if($res&&$res2){
            Db::commit();
            return  json(array('status'=>200,'msg'=>$res));
        }else{
            Db::rollback();
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
    //开礼物
    public function kaiPresent(){
        extract(input());
        $id = input('param.id');
        $res = Db::table('up_present')->where('present_id',$id)->find();
        if($res['present_status']==2){
            return  json(array('status'=>100,'msg'=>"礼物已经拆过，不能重复拆开"));
        }
        $price = Db::table('up_present')->where('present_id',$id)->value('present_glod');
        //被申请人要加积分
        Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_integral',$price);
        //更新被申请人账户余额
        Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_balance',$price);
        //更新被聊天人收到礼物
        Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_gift_gold',$price);
        Db::table('up_present')->where('present_id',$id)->setField('present_status',2);
        return  json(array('status'=>200,'msg'=>"成功"));
    }
}